package com.xiaoqi.store.service.impl;

import com.xiaoqi.store.dao.ProductMapper;
import com.xiaoqi.store.entity.ProductEntity;
import com.xiaoqi.store.exception.impl.ProductNotFoundHot;
import com.xiaoqi.store.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Rookie Coder
 * @Description 商品业务层
 * @Date
 **/
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    /*
    * @Author xiaoqi
    * @Description  查询所有的热销商品
    * @return List<Product>
    **/
    @Override
    public List<ProductEntity> selectAllHotProduct() {
        // 查询所有的热销商品
        List<ProductEntity> hotProductEntityList =  productMapper.selectAllHotProduct();
        // 判断查询出来的热销商品是否为空
        if (hotProductEntityList == null){
            log.info("找不到热销商品");
            throw new ProductNotFoundHot("找不到热销商品");
        }
        return hotProductEntityList;
    }

    /*
    * @Author xiaoqi
    * @Description  查询商品详情
    * @return
    **/
    @Override
    public ProductEntity selectProductDetails(Integer id) {
        log.info("商品id"+ id);
        ProductEntity productDetail = productMapper.selectProductDetail(id);
        log.info("商品详情： "+productDetail);

        if (productDetail == null){
            log.info("该商品不存在");
            throw new ProductNotFoundHot("该商品不存在");
        }
        // 判断当前商品是否下架
        if (productDetail.getStatus() == 2 ){
            log.info("该商品已下架");
            throw new ProductNotFoundHot("该商品已下架");
        }
        if (productDetail.getStatus() == 3 ){
            log.info("该商品已删除");
            throw new ProductNotFoundHot("该商品已下架");
        }
        return productDetail;
    }
}
